#!/usr/bin/perl -w

use strict;
use v5.14;
use utf8;

use Spreadsheet::ParseExcel;
use XML::Writer;
use IO::File;

binmode(STDIN,":utf8");
binmode(STDOUT,":utf8");
binmode(STDERR,":utf8");

@ARGV == 1 || die "You must provide input file.";
my $file = $ARGV[0];
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($file);
$workbook || die $parser->error();

my $output = IO::File->new('>output.xml');
$output->binmode(":utf8");
my $writer = XML::Writer->new(OUTPUT=>$output,DATA_MODE=>1,DATA_INDENT=>' ');
$writer->xmlDecl("UTF-8");
$writer->startTag("quiz");

for my $worksheet ($workbook->worksheets()) {
  my $funcname = $worksheet->get_name;
  if ($::{$funcname}) {
    $::{$funcname}->($writer,$worksheet);
  }
}

$writer->endTag("quiz");
$writer->end();
$output->close();

sub single_choice(){
  my ($writer,$sheet) = @_;
  my ($row_min,$row_max) = $sheet->row_range();
  for my $row ($row_min..$row_max) {
    #忽略第0行，这是标题行
    next if $row == 0;
    $writer->startTag('question', type=>'multichoice');
    #第0列为标题
    my $title = $sheet->get_cell($row,0)->value();
    gen_title($title,$writer);
    #处理答案，答案可能使用小写，统一转换成大写
    my $answer = uc($sheet->get_cell($row,5)->value());
    for (my $i=1; $i<=4; $i++) {
      my $option = $sheet->get_cell($row,$i)->value();
      my $fvalue = fraction($i,$answer);
      $writer->startTag('answer', fraction=>$fvalue);
      $writer->cdataElement('text',$option);
      $writer->startTag('feedback');
      my $feedtxt = $fvalue==100 ? '恭喜你，答对了！' : '可惜了，再仔细想想！';
      $writer->dataElement('text',$feedtxt);
      $writer->endTag('feedback');
      $writer->endTag('answer');
    }

    $writer->dataElement('shuffleanswers',0);
    $writer->dataElement('single', 'true');
    $writer->dataElement('answernumbering','ABCD');
    $writer->endTag('question');
  }

}

#根据列序号和答案，计算fraction，100或0
sub fraction(){
  my ($col,$answer) = @_;
  my $correct = index('ABCD',$answer) + 1;
  $correct == $col ? 100 : 0;
}

sub shortanswer(){
  my ($writer,$sheet) = @_;
  my ($row_min,$row_max) = $sheet->row_range();
  for my $row ($row_min..$row_max) {
    #忽略第0行，这是标题行
    next if $row == 0;
    $writer->startTag('question', type=>'shortanswer');
    #第0列为标题
    my $title = $sheet->get_cell($row,0)->value();
    gen_title($title,$writer);

    my $answer = $sheet->get_cell($row,1)->value();
    $writer->startTag('answer',fraction=>100);
    $writer->cdataElement('text',$answer);
    $writer->startTag('feedback');
    $writer->dataElement('text','回答正确！');
    $writer->endTag('feedback');
    $writer->endTag('answer');
    
    $writer->endTag('question');
  }
}

sub essay(){
  my ($writer,$sheet) = @_;
  my ($row_min,$row_max) = $sheet->row_range();
  for my $row ($row_min..$row_max) {
    #忽略第0行，这是标题行
    next if $row == 0;
    $writer->startTag('question', type=>'essay');
    #第0列为标题
    my $title = $sheet->get_cell($row,0)->value();
    gen_title($title,$writer);

    my $answer = $sheet->get_cell($row,1)->value();
    $writer->startTag('answer',fraction=>100);
    $writer->cdataElement('text',$answer);
    $writer->endTag('answer');
    
    $writer->endTag('question');
  }
}

sub truefalse(){
  my ($writer,$sheet) = @_;
  my ($row_min,$row_max) = $sheet->row_range();
  for my $row ($row_min..$row_max) {
    #忽略第0行，这是标题行
    next if $row == 0;
    $writer->startTag('question', type=>'truefalse');
    #第0列为标题
    my $title = $sheet->get_cell($row,0)->value();
    gen_title($title,$writer);

    my $answer = $sheet->get_cell($row,1)->value();
    my ($tfraction,$ffraction);
    if ($answer eq 'T') {
      $tfraction = 100;
      $ffraction = 0;
    }
    else {
      $tfraction = 0;
      $ffraction = 100;
    }
    $writer->startTag('answer',fraction=>$tfraction);
    $writer->dataElement('text','true');
    $writer->startTag('feedback');
    $writer->dataElement('text','回答正确！');
    $writer->endTag('feedback');
    $writer->endTag('answer');
    
    $writer->startTag('answer',fraction=>$ffraction);
    $writer->dataElement('text','false');
    $writer->startTag('feedback');
    $writer->dataElement('text','可惜了，再仔细想想！');
    $writer->endTag('feedback');
    $writer->endTag('answer');

    $writer->endTag('question');
  }
}

sub gen_title(){
  my ($title,$writer) = @_;
  #有的标题可能会用空格开头，删除开始位置的空格
  $title =~ s/^\s+//;
  $writer->startTag('name');
  #标题可能有多行，只取第一行作为name
  my $line = (split /\s+/,$title)[0];
  $writer->dataElement('text',$line);
  $writer->endTag('name');
  $writer->startTag('questiontext',format=>'moodle_auto_format');
  $writer->cdataElement('text',$title);
  $writer->endTag('questiontext');
}

